package com.ColonyCount.View;

import com.ColonyCount.ColonyCountHeap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;

public class RegionOfInterestView extends View {
	
	Bitmap background;
    float lastTouchX;
    float lastTouchY;
    
    ROISphere roi;
    ROIBase selectedRegion;    
    Rect clipBounds;
    Bitmap overlayBitmap;
    float threshold;
    
	public RegionOfInterestView(Context context) 
	{
		super(context);
	}
	
	public RegionOfInterestView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }
	
	public void setup(Bitmap background, Rect clipBounds, float threshold)
	{
		this.background = background;
		this.clipBounds = clipBounds;
		this.threshold = threshold;
		
		if(background != null)
		{
			roi = null;
			invalidate();
		}
	}

	@Override
	protected void onDraw (Canvas canvas)
	{
		if(background == null)
			return;
		
		if(roi == null)
		{
			float scale = (float)getWidth()/(float)background.getWidth();
			RectF bounds = new RectF(clipBounds.left*scale,
									clipBounds.top*scale,
									clipBounds.right*scale,
									clipBounds.bottom*scale);
			DisplayMetrics dm = getResources().getDisplayMetrics();
			roi = new ROISphere(dm.density, bounds);
			
			overlayBitmap = Bitmap.createBitmap(getWidth(),(int)  getHeight(), Bitmap.Config.ARGB_4444);
			
		}
		
		
		
		
		
		// Draw background
    	Paint paint = new Paint();
    	paint.setAntiAlias(false);
    	paint.setFilterBitmap(false);
    	paint.setDither(false);
    	paint.setColor(Color.WHITE);
		float aspect = (float)background.getHeight()/(float)background.getWidth();
		Rect targetRect = new Rect(0,0, getWidth(), (int)((float)getWidth()*aspect));
		Rect sourceRect = new Rect(0,0,background.getWidth(),background.getHeight());
		canvas.drawBitmap(background, sourceRect, targetRect, paint);

		
		overlayBitmap.eraseColor(0x00000000);
		Canvas overlayCanvas = new Canvas(overlayBitmap);
		
		// Draw threshold on overlay
		Paint thresholdPaint = new Paint();
		float[] array = { 	
				0, 0, 0, 0, 50, 
				0, 0, 0, 0, 255,
				0, 0, 0, 0, 50,
				-128, 0, 0, 0, 128*threshold };
		ColorMatrix colorMatrix = new ColorMatrix(array);
		thresholdPaint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
		overlayCanvas.drawBitmap(background, sourceRect, targetRect, thresholdPaint);
	
		// Draw ROI filter on overlay
    	roi.drawFilterPreview(overlayCanvas);
    	
    	// Draw overlay
		canvas.drawBitmap(overlayBitmap, 0, 0, paint);

		// Draw filter gizmo
		roi.drawFilterGizmoes(canvas);
	}

	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		float x = event.getX();
		float y = event.getY();
		switch(event.getAction())
		{
		case MotionEvent.ACTION_DOWN:
			findSelectedRegion(x, y);
			break;
		case MotionEvent.ACTION_MOVE:
			float deltaX = x - lastTouchX;
			float deltaY = y - lastTouchY;
			if(selectedRegion != null)
			{
				selectedRegion.move(deltaX, deltaY);
				invalidate();
			}
			else
			{
				findSelectedRegion(x, y);
			}
			break;
		case MotionEvent.ACTION_UP:
			selectedRegion = null;
			break;
		}
		lastTouchX = x;
		lastTouchY = y;
		return true;
	}
	
	void findSelectedRegion(float x, float y)
	{
		if(roi.select(x, y))
		{
			selectedRegion = roi;
		}
	}

	public Rect getRegionBounds()
	{
		float scale = (float)background.getWidth()/(float)getWidth();
		return new Rect((int)(roi.getBounds().left*scale),
						(int)(roi.getBounds().top*scale),
						(int)(roi.getBounds().right*scale),
						(int)(roi.getBounds().bottom*scale));
	}
}
